﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：DESKTOP-UL84BU5
 * 公司名称：
 * 命名空间：KingerRobot.PCRBW.Biz.Tool
 * 唯一标识：da5a8604-e3a8-4536-8b81-03f17f61a506
 * 文件名：Tool
 * 当前用户域：DESKTOP-UL84BU5
 * 
 * 创建者：zzl
 * 电子邮箱：1003590782@qq.com
 * 创建时间：2022/8/24 12:36:18
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using System;

namespace KingerRobot.PCRBW.Biz;

internal class OctalTool
{
    /// <summary>
    /// 十进制转换Ascii
    /// </summary>
    /// <param name="param_value"></param>
    /// <param name="param_value2"></param>
    /// <returns></returns>
    public static string GetIntToAscii(string param_value, string param_value2, int Num = 100)
    {
        _ = int.TryParse(param_value, out int value);
        _ = int.TryParse(param_value2, out int value2);

        if (value == 0 && value2 == 0)
        {
            return "";
        }

        var value_first = value % Num;
        var value_second = (value - value_first) / Num;
        var value_first_str = ((char)value_first).ToString();
        var value_second_str = ((char)value_second).ToString();

        var value2_first = value2 % Num;
        var value2_second = (value2 - value2_first) / Num;
        var value2_first_str = ((char)value2_first).ToString();
        var value2_second_str = ((char)value2_second).ToString();


        return value_first_str + value_second_str + value2_first_str + value2_second_str;
    }


    /// <summary>
    /// Ascii 转换十进制
    /// </summary>
    /// <param name="first"></param>
    /// <param name="second"></param>
    /// <returns></returns>
    public static int GetMergeAsciiToInt(char? first, char? second)
    {
        if (first is null || second is null)
        {
            return 0;
        }
        return GetValueSum(GetAsciiToInt(first), GetAsciiToInt(second));
    }

    public static (int, int, int, int) GetAsciiToByte(string barCode)
    {
        if (string.IsNullOrWhiteSpace(barCode))
        {
            return (0, 0, 0, 0);
        }

        var bt = ASCIIEncoding.ASCII.GetBytes(barCode);
        var first = GetValueSum(bt[0], bt[1]);
        var second = GetValueSum(bt[2], bt[3]);
        var third = GetValueSum(bt[4], bt[5]);
        var four = GetValueSum(bt[6], bt[7]);

        return (first, second, third, four);
    }


    public static int GetAsciiToInt(char? first)
    {
        var first_value = (int)first;
        return first_value;
    }


    private static int GetValueSum(int first, int second, int Num = 100)
    {
        return second * Num + first;
    }

    /// <summary>
    /// 十进制转二进制 不足左侧补位 0
    /// </summary>
    /// <param name="num"></param>
    /// <param name="padLeft"></param>
    /// <returns></returns>
    public static string GetPadLeftIntToBinary(int num, int padLeft = 8)
    {
        return Convert.ToString(num, 2).PadLeft(padLeft, '0');
    }
    /// <summary>
    /// 十进制转二进制 不足右侧补位 0
    /// </summary>
    /// <param name="num"></param>
    /// <param name="padLeft"></param>
    /// <returns></returns>
    public static string GetPadRightIntToBinary(int num, int padRight = 8)
    {
        return Convert.ToString(num, 2).PadLeft(padRight, '0');
    }
}
